/*
	Pinkie's Balloon Fight
	Copyright (C) 2012  dancinninja (dancinninjac@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "TexManager.h"

//----------------------------------------
// Default Constructor
//----------------------------------------

TexManager::TexManager(){}

//----------------------------------------
// Destructor
//----------------------------------------

TexManager::~TexManager(){
    while(!vTexture.empty()){
		delete vTexture.back();
        vTexture.pop_back();
    }
}

//----------------------------------------
//
// Function: LoadTexture
//
//		Loads in a texture and pushes it onto an array
//
// @pre     -
// @post    The texture has been created and pushed
// @param   fileName: the texture to load and push 
// @return  true if successful
//
//----------------------------------------
#ifdef WIN32

#include "win/bmp.h"
#endif

bool TexManager::LoadTexture(const char* fileName){

	Texture* texture = new Texture;

#ifdef WIN32
	AUX_RGBImageRec* TextureImage = LoadBMP(fileName);

	if (TextureImage){
		glGenTextures(1, &texture->obj);
		// Create Linear Filtered Texture
		glBindTexture(GL_TEXTURE_2D, texture->obj);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		
		vTexture.push_back(texture);

		if (TextureImage->data){
			free(TextureImage->data);
		}
		free(TextureImage);

		return true;
	}
	return false;

#else
	//Wii

	// Open the PNG
	FILE *file = fopen(fileName, "rb");

	if(!file){
		fprintf(stderr, "Error: %s is an invalid texture\n", fileName);
		return false;
	}

	fseek(file, 0, SEEK_END);
	u32 imageSize = ftell(file);
	rewind(file);

	u8* tempData = (u8*)(std::memalign(32, imageSize));
	if(!tempData){
		fprintf(stderr, "Couldn't allocate data for a texture\n");
		return false;
	}

	fread(tempData, 1, imageSize, file);

	s32 width;
	s32 height;

	texture.data = DecodePNG(tempData, &width, &height, NULL, 0,0);

	GX_InitTexObj(&texture.obj, texture.data, width, height, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);

	free(tempData);
	fclose(file);

	return true;
#endif

}

//----------------------------------------
//
// Function: GetTexture
//
//		Returns a texture at the given index
//
// @pre     index should be within the bounds of our texture vector
// @post    -
// @param   index: which texture to return (by number)
// @return  Pointer to the texture
//
//----------------------------------------

Texture* TexManager::GetTexture(s32 index) const{

	if (u32(index) >= vTexture.size())
        return NULL;

    return (vTexture.at(index));
}

//----------------------------------------
//
// Function: Size
//
//		Get the size of our texture vector
//
// @pre     -
// @post    -
// @param   -
// @return  The size of the texture vector
//
//----------------------------------------

s32 TexManager::GetSize() const{
    return vTexture.size();
}

//----------------------------------------
//
// Function: Clear
//
//		Empty out the texture vector
//
// @pre     -
// @post    vTexture is now empty
// @param   -
// @return  -
//
//----------------------------------------

void TexManager::Clear(){
    while(!vTexture.empty()){
		delete vTexture.back();
        vTexture.pop_back();
    }
}

//----------------------------------------
//
// Function: Reserve
//
//		Reserve the amount of textures we will be adding
//
// @pre     -
// @post    vTexture now has the appropriate amount of spots
// @param   -
// @return  -
//
//----------------------------------------

void TexManager::Reserve(u32 amount){
    vTexture.reserve(amount);
}
